//
// Created by zy-li14 on 16-10-7.
//
#include "sdk/const.h"
#include "sdk/interpreter.h"
#include "include/sdk/Commands.h"
#include <iostream>
#include "json/json.h"
#include <fstream>
#include "sdk/misc.h"
#include <sdk/PlayerInfo.h>

using namespace std;

enum mode{
    BlankMode,
    MirrorMode
};
extern "C"
{
__declspec(dllexport) void player_ai(const PlayerInfo &playerInfo, Commands &pCommand, SDK::PShowInfo &pShowInfo_) {
    //std::cout << "I am in player_ai" << std::endl;
    static mode state = BlankMode;
    static string mirrorFileName;

    for (auto &data: playerInfo.push_data) {
        Json::Value root = stringToJson(data);
        if (root["action"] == "changemode") {
            if (root["params"][0] == "mirror") {
                state = MirrorMode;
                mirrorFileName = root["params"][1].asString();
            }
        }
    }
    SDK::PPlayerInfo pPlayerInfo = SDK::interpretPlayerInfoStatus(stringToJson(playerInfo.status));

    if (state == MirrorMode) {
        static bool readed = false;
        static Json::Value actions;
        if (!readed) {
            ifstream ifs(mirrorFileName);
            Json::CharReaderBuilder rbuilder;
            rbuilder["collectComments"] = false;
            std::string errs;
            bool ok = Json::parseFromStream(rbuilder, ifs, &actions, &errs);
        }
        Json::Value status = stringToJson(playerInfo.status);
        int round = status["round"].asInt();
        for (int i = 0; i < actions[round].size(); ++i) {
            Json::Value &action = actions[round][i];
            Command command;
            command.name = action[0].asString();
            for (int j = 1; j < action.size(); ++j)
                command.params.push_back(action[j].asString());
            pCommand.cmds.push_back(command);
        }
    }
}
};